package com.binguo.wxapi.controller;

import com.binguo.common.enums.CardType;
import com.binguo.common.exception.ServiceException;
import com.binguo.helper.FaceHelperV3;
import com.binguo.model.model.PayCard;
import com.binguo.model.model.Shop;
import com.binguo.model.model.ShopOnlineMeal;
import com.binguo.model.wxapi.vo.ShopVO;
import com.binguo.model.wxapi.vo.ShopsVO;
import com.binguo.service.PayService;
import com.binguo.service.ShopService;
import com.binguo.wxapi.form.CustomerForm;
import com.binguo.wxapi.form.ShopMeshConfigForm;
import com.binguo.wxapi.form.ShopReservePayForm;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.service.WXCustomerService;
import com.binguo.wxapi.transfer.WXShopsTransfer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


/**
 * Created by haibang on 18/5/7.
 */
@Api(tags = "小程序: 商户")
@RestController
@RequestMapping
public class WXShopController {

    private static Logger logger = LoggerFactory.getLogger(WXShopController.class);


    @Autowired
    ShopService shopService;

    @Autowired
    WXShopsTransfer WXShopsTransfer;

    @Autowired
    PayService payService;

    @Autowired
    WXCustomerService wxCustomerService;

    private String menuName = "人脸列表";

    @GetMapping(value = "/api/wxapp/shop/name/face", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "刷新", notes = "")
    public Result face(){
        menuName = "人脸列表";
        return ResultGenerator.genSuccessResult(menuName);
    }

    @GetMapping(value = "/api/wxapp/shop/name/info", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "刷新", notes = "")
    public Result info(){
        menuName = "云澎简介";
        return ResultGenerator.genSuccessResult(menuName);
    }

    @PostMapping(value = "/api/wxapp/shop/enable", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询用户有权限上传人脸", notes = "")
    public Result customerEnable(@RequestBody @Valid CustomerForm form){
//        boolean result = wxCustomerService.searchUserEnableUploadFace(form.getCustomerId());

//        return ResultGenerator.genSuccessResult("云澎简介");
        return ResultGenerator.genSuccessResult(menuName);
    }

    @PostMapping(value = "/api/wxapp/shop/meshorder/shopList", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询用户可订餐商户列表", notes = "")
    public Result shopList(@RequestBody @Valid CustomerForm form){
        List<ShopVO> shopVOList = shopService.getMeshShopList(form.getCustomerId());


        if (shopVOList != null && shopVOList.size() > 0) {
            return ResultGenerator.genSuccessResult(shopVOList);
        } else {
            if (shopVOList == null) {
                shopVOList = shopService.getMeshShopList(74);
                return ResultGenerator.genSuccessResult(shopVOList);
            } else {
                return ResultGenerator.genFailResult("商户未开通订餐功能");
            }
        }
    }

    @PostMapping(value = "/api/wxapp/shop/meshorder/config", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商户订餐配置", notes = "")
    public Result shopConfig(@RequestBody @Valid ShopMeshConfigForm form){
        ShopOnlineMeal shopOnlineMeal =
                shopService.shopMeshConfig(form.getShopId(), form.getAutoConfig(), form.getBreakfastStatus(),
                    form.getLunchStatus(), form.getDinnerStatus()
                );
        if (shopOnlineMeal != null) {
            return ResultGenerator.genSuccessResult(shopOnlineMeal);
        } else {
            throw new ServiceException("参数错误");
        }

    }

    @PostMapping(value = "/api/wxapp/shop/reservePay/config", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商户线下/线上支付配置", notes = "")
    public Result shopReservePayConfig(@RequestBody @Valid ShopReservePayForm form){

        shopService.shopReservePayConfig(form.getShopId(),form.getReservePayConfig());

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping(value = "/api/wxapp/shop/create/{shopId}", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商户创建", notes = "商户创建")
    public Result list(@PathVariable Integer shopId){
        FaceHelperV3.createGroup(shopId.toString());
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping(value = "/api/wxapp/shop/list/{customerId}", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商户列表", notes = "商户列表")
    public Result list(@PathVariable Integer customerId,
                       @ApiParam("是否有一卡通") @RequestParam(defaultValue = "false") Boolean hasYKT){
       if(hasYKT){
           List<Shop> shops = shopService.find(hasYKT);
           List<PayCard> payCards =  payService.getPayCardByCustomerId(customerId, CardType.YKT_CARD);
           List<Integer> shopIds = payCards.stream().map(PayCard::getShopid).collect(Collectors.toList());
           List<Shop> newShops = shops.stream().filter(shop -> !shopIds.contains(shop.getId())).collect(Collectors.toList());
           List<ShopsVO> vos = WXShopsTransfer.toShopsVOList(newShops);
           return ResultGenerator.genSuccessResult(vos);
       } else {
           List<Shop> shops = shopService.find();
           List<PayCard> payCards =  payService.getPayCardByCustomerId(customerId,  CardType.BINGUO_CARD);
           List<Integer> shopIds = payCards.stream().map(PayCard::getShopid).collect(Collectors.toList());
           List<Shop> newShops = shops.stream().filter(shop -> !shopIds.contains(shop.getId())).collect(Collectors.toList());
           List<ShopsVO> vos = WXShopsTransfer.toShopsVOList(newShops);
           return ResultGenerator.genSuccessResult(vos);
       }


    }




    @GetMapping(value = "/api/wxapp/shop/list/search/{customerId}", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "搜索商户列表", notes = "商户列表")
    public Result searchList(@PathVariable Integer customerId,
                       @ApiParam("是否有一卡通") @RequestParam(defaultValue = "false") String  name,
                       @ApiParam("是否有一卡通") @RequestParam(defaultValue = "false") Boolean hasYKT){
       if(hasYKT){
           List<Shop> shops = shopService.findByName(hasYKT, name);
           List<PayCard> payCards =  payService.getPayCardByCustomerId(customerId, CardType.YKT_CARD);
           List<Integer> shopIds = payCards.stream().map(PayCard::getShopid).collect(Collectors.toList());
           List<Shop> newShops = shops.stream().filter(shop -> !shopIds.contains(shop.getId())).collect(Collectors.toList());
           List<ShopsVO> vos = WXShopsTransfer.toShopsVOList(newShops);
           return ResultGenerator.genSuccessResult(vos);
       } else {
           List<Shop> shops = shopService.findByName(name);
           List<PayCard> payCards =  payService.getPayCardByCustomerId(customerId,  CardType.BINGUO_CARD);
           List<Integer> shopIds = payCards.stream().map(PayCard::getShopid).collect(Collectors.toList());
           List<Shop> newShops = shops.stream().filter(shop -> !shopIds.contains(shop.getId())).collect(Collectors.toList());
           List<ShopsVO> vos = WXShopsTransfer.toShopsVOList(newShops);
           return ResultGenerator.genSuccessResult(vos);
       }


    }






}
